//
//  SXTLandingNextViewController.m
//  SXT
//
//  Created by 赵金鹏 on 16/8/15.
//  Copyright © 2016年 赵金鹏. All rights reserved.
//

#import "SXTLandingNextViewController.h"
#import "SXTNextView.h"//下一步view

@interface SXTLandingNextViewController ()<UITextFieldDelegate>

@property (strong, nonatomic)   SXTNextView *nextView;              /** 下一步view */

@end

@implementation SXTLandingNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = 0;
    self.view.backgroundColor = MainColor;
    self.title = @"验证手机号";
    [self addController];
    [self startCreateCodeMethod];
}



//加载控件
- (void)addController{
    [self.view addSubview:self.nextView];
    __weak typeof (self) weakSelf = self;
    [_nextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.left.equalTo(weakSelf.view);
        make.height.equalTo(@140);
    }];
}

//获取验证码网络请求
- (void)startCreateCodeMethod{
    
    [self POSTRequestURL:@"appMember/createCode.do"
                   param:@{@"MemberId":_userDic[@"userName"]}
               isSuccess:^(id responseObject) {
                   
                   if ([responseObject[@"result"] isEqualToString:@"success"]) {
                       [_nextView GCDTime];
                   }else if ([responseObject[@"result"] isEqualToString:@"TelephoneExistError"]){
                       [self showTostInView:@"手机号已注册"];
                   }else{
                       [self showTostInView:@"手机号错误"];
                   }
                   
                   SXTLog(@"responseObject = %@",responseObject);
                   
               } isError:^(NSError *error) {
        
    }];
}

- (void)LandingButtonMethod:(NSString *)codeString{
    
    [self GETRequestURL:@"appMember/appRegistration.do" param:@{@"LoginName":_userDic[@"userName"],
                                                               @"Lpassword":_userDic[@"password"],
                                                               @"Code":codeString,
                                                               @"Telephone":_userDic[@"userName"]}
              isSuccess:^(id responseObject) {
                                   SXTLog(@"注册：%@",responseObject);
                                   if ([responseObject[@"result"] isEqual:@"success"]) {
                                       [self showTostInView:@"注册成功"];
                                       [self performSelector:@selector(returnMyview) withObject:nil afterDelay:1.0];
                                   }else if([responseObject[@"result"] isEqual:@"codeError"]){
                                       [self showTostInView:@"验证码错误"];
                                   }else{
                                       [self showTostInView:@"注册失败"];
                                   }
                               }
                isError:^(NSError *error) {
                                   [self showTostInView:@"请检查网络"];
                           }];
}

- (SXTNextView *)nextView{
    if (!_nextView) {
        _nextView = [[SXTNextView alloc]init];
        _nextView.phoneNumText = _userDic[@"userName"];
        __weak typeof (self) weakSelf = self;
        _nextView.showTimeBlock = ^(){
            [weakSelf startCreateCodeMethod];
        };
        _nextView.landingBtnBlock = ^(NSString *codeString){
            [weakSelf LandingButtonMethod:codeString];
        };
    }
    return _nextView;
}

- (void)returnMyview{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
